package cn.zhengzhou.springbootmybatis.controller;

import cn.zhengzhou.springbootmybatis.mapper.StudentMapper;
import cn.zhengzhou.springbootmybatis.pojo.Student;
import cn.zhengzhou.springbootmybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController//@Controller 和 @ResponseBody
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 添加学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("/add")
    public Object addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("data", student);
        return map;
    }

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    @PostMapping("/update")
    public Object updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("data", student);
        return map;
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Object delStudent(String id) {
        studentService.delStudent(id);
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("data", "删除学生id为："+id);
        return map;
    }

    /**
     * 查询学生信息
     * @return
     */
    @GetMapping("/query")
    public Object queryStudent() {
        List<Student> list=  studentService.queryStudent();
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("data", list);
        return map;
    }
}
